package com.xiaoze.springcloud.service.impl;

import com.xiaoze.springcloud.dao.UserDao;
import com.xiaoze.springcloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author xiaoze
 * @date 2020/5/28
 */
@Service
public class UserServiceDetailImpl implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.get(username);
        if (user == null) {
            throw new BadCredentialsException("user: " + username + " not found.");
        }
        return new org.springframework.security.core.userdetails.User(user.getUserNo(), user.getUserPwd(),
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER,ROLE_ADMIN"));
    }
}
